package com.example.excelapp.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.excelapp.entity.UserData;
import com.example.excelapp.mapper.UserMapper;
import com.example.excelapp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Time;
import java.util.List;

@RestController
@RequestMapping("/excel")
public class ExcelController {

    @Autowired
    private UserService userService;
    @GetMapping("/generate")
    public String generateAndInsertMillionData() {
        userService.generateAndInsertMillionData();
        return "Ok";
    }
    @GetMapping("/export")
    public String exportExcel() {
        userService.exportExcel();
        return "Ok";
    }

    @GetMapping("/import")
    public String importExcel() throws IOException {

        String filePath = "E:\\Desktop\\users.xlsx";
        MultipartFile multipartFile = ExcelController.convertFileToMultipartFile(filePath);
        userService.importExcel(multipartFile);

        return "Ok";
    }

    @GetMapping("/export2")
    public String exportMan() throws IOException  {
        System.out.println("开始导出222222");
        userService.exportExcelMan();
        return "Ok";
    }
    public static MultipartFile convertFileToMultipartFile(String filePath) throws IOException {
        File file = new File(filePath);
        InputStream inputStream = new FileInputStream(file);
        MultipartFile multipartFile = new MockMultipartFile(
                "file",
                file.getName(),
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                inputStream
        );
        return multipartFile;
    }
}

